home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Book Chapters
/
10 - Networking
/
NovelNetwar
/
dialogs.c
< prev
next >
Wrap
Text File
|
1995-05-12
|
9KB
|
484 lines
// This module takes care of dialog-box related stuff.
#include "NovelNetwar.h"
#include <GestaltEqu.h>
#include <Traps.h>
#include <CTBUtilities.h>
#include <EPPC.h>
void FatalError(char *fatalString)
{
ErrorAlert(fatalString);
DoShutDown();
ExitToShell();
}
void ErrorAlert(char *p0)
{
GrafPtr oldPort;
DialogPtr theDPtr,tempDPtr;
short itemHit;
short type;
Handle theItem;
Rect theRect;
EventRecord theEvent;
GetPort(&oldPort);
InitCursor();
if (!(theDPtr = GetNewDialog(ERRORDLOG, nil,(WindowPtr) -1L)))
{
SysBeep(1);
}
SetPort(theDPtr);
CenterWindow(theDPtr);
ShowWindow(theDPtr);
((DialogPeek) theDPtr)->aDefItem = 1;
GetDItem(theDPtr, 2, &type, &theItem, &theRect);
SetDItem(theDPtr, 2, type, (Handle) HiliteDefaultButton, &theRect);
GetDItem(theDPtr, 4, &type, &theItem, &theRect);
CtoPstr(p0);
SetIText(theItem,(unsigned char *) p0);
PtoCstr((unsigned char *) p0);
if (GetNextEvent(updateMask,&theEvent))
{
if (theEvent.what == kHighLevelEvent)
{
DoHighLevel(&theEvent);
}
else if (theEvent.message == (long) theDPtr)
{
DialogSelect(&theEvent,&tempDPtr,&itemHit);
}
}
SysBeep(1);
do
{
ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
} while (itemHit != 1);
DisposDialog(theDPtr);
SetPort(oldPort);
}
// This is my custom dialog event preprocessor. It does handy things like intercept the escape
// key and translate it into a click on a Cancel button (if there is one), translate command-key
// events into mouseclicks on buttons, etc.
OSErr ProcessDialogCmdKey(DialogPtr theDialog,EventRecord *theEvent,short *itemHit)
{
char ch,tempString[256];
int i,j,length,numItems;
Handle itemHandle;
Rect dispRect;
long theTicks;
long hasDITLExtensions;
short itemType;
int theResult;
theResult = false;
if (theEvent->what == keyDown || theEvent->what == autoKey)
{
ch = theEvent->message & charCodeMask;
if (ch >= 'a' && ch <= 'z')
ch = 'A' + ch - 'a';
if (((DialogPeek) theDialog)->aDefItem > 0 && (ch == '\r' || ch == '\n' || ch == 0x03))
{
GetDItem(theDialog,((DialogPeek) theDialog)->aDefItem,&itemType,&itemHandle,&dispRect);
if ((**(ControlHandle) itemHandle).contrlHilite != 255)
{
HiliteControl((ControlHandle) itemHandle,inButton);
theTicks = TickCount();
while (TickCount() < theTicks + 15);
HiliteControl((ControlHandle) itemHandle,0);
*itemHit = ((DialogPeek) theDialog)->aDefItem;
theResult = true;
goto EXITPOINT;
}
}
if ((theEvent->modifiers & cmdKey) && ((DialogPeek) theDialog)->editField != -1 && ((DialogPeek) theDialog)->textH != 0L)
{
switch (ch)
{
case 'C':
DlgCopy(theDialog);
*itemHit = -1;
theResult = true;
goto EXITPOINT;
break;
case 'V':
DlgPaste(theDialog);
*itemHit = -1;
theResult = true;
goto EXITPOINT;
break;
case 'X':
DlgCut(theDialog);
*itemHit = -1;
theResult = true;
goto EXITPOINT;
break;
}
}
if ((theEvent->modifiers & cmdKey) || (((DialogPeek) theDialog)->editField == -1) || (((DialogPeek) theDialog)->textH == 0L) || ch == 0x1b)
{
numItems = -1;
if (TrapAvailable((int) _Gestalt) == noErr)
{
if (Gestalt(gestaltDITLExtAttr,&hasDITLExtensions) == noErr)
{
if (hasDITLExtensions & (1 << gestaltDITLExtPresent))
{
numItems = CountDITL(theDialog);
}
}
}
if (numItems >= 1)
{
for (i=1;i<=numItems;i++)
{
GetDItem(theDialog,i,&itemType,&itemHandle,&dispRect);
if (itemType == ctrlItem + btnCtrl && itemHandle)
{
GetCTitle((ControlHandle) itemHandle,(unsigned char *) tempString);
if (ch == 0x1B || ch == '.')
{
PtoCstr((unsigned char *) tempString);
if (strcmp(tempString,"Cancel") == EQSTR)
{
GetDItem(theDialog,i,&itemType,&itemHandle,&dispRect);
if ((**(ControlHandle) itemHandle).contrlHilite != 255)
{
HiliteControl((ControlHandle) itemHandle,inButton);
theTicks = TickCount();
while (TickCount() < theTicks + 15);
HiliteControl((ControlHandle) itemHandle,0);
*itemHit = i;
theResult = true;
goto EXITPOINT;
}
}
}
else
{
length = tempString[0];
for (j=1;j<=length;j++)
{
if (tempString[j] >= 'A' && tempString[j] <= 'Z')
{
if (tempString[j] == ch)
{
GetDItem(theDialog,i,&itemType,&itemHandle,&dispRect);
if ((**(ControlHandle) itemHandle).contrlHilite != 255)
{
HiliteControl((ControlHandle) itemHandle,inButton);
theTicks = TickCount();
while (TickCount() < theTicks + 15);
HiliteControl((ControlHandle) itemHandle,0);
*itemHit = i;
theResult = true;
goto EXITPOINT;
}
}
else
j = length + 1;
}
}
}
}
}
if (ch != 'X' && ch != 'C' && ch != 'V')
SysBeep(1);
}
}
}
EXITPOINT:
return(theResult);
}
pascal Boolean MyFilter(DialogPtr theDialog,EventRecord *theEvent,short *itemHit)
{
Boolean theResult;
theResult = ProcessDialogCmdKey(theDialog,theEvent,itemHit);
return(theResult);
}
void CenterWindow(WindowPtr wPtr)
{
int screenWidth,screenHeight,windowWidth,windowHeight,left,top;
if (wPtr == 0L)
return;
screenWidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
screenHeight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - 40;
windowWidth = wPtr->portRect.right - wPtr->portRect.left;
windowHeight = wPtr->portRect.bottom - wPtr->portRect.top + 40;
left = qd.screenBits.bounds.left + (screenWidth - windowWidth)/2;
top = qd.screenBits.bounds.top + 55 + (screenHeight - windowHeight)/2;
if (left < 0)
left = 0;
if (top < 55)
top = 55;
MoveWindow(wPtr,left,top,false);
}
pascal void HiliteDefaultButton(DialogPtr theDPtr,int whichItem)
{
short type;
Handle theItem;
Rect theRect;
PenState thePenState;
GrafPtr oldPort;
if (theDPtr)
{
GetPort(&oldPort);
SetPort(theDPtr);
GetPenState(&thePenState);
GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
PenNormal();
PenSize(3,3);
InsetRect(&theRect,-4,-4);
FrameRoundRect(&theRect,16,16);
PenSize(1,1);
SetPenState(&thePenState);
SetPort(oldPort);
}
}
OSErr GetInput(char *thePrompt,char *theText)
{
DialogPtr theDPtr,tempDPtr;
short itemHit, type;
Handle theItem;
Rect theRect;
GrafPtr oldPort;
EventRecord theEvent;
GetPort(&oldPort);
InitCursor();
if (!(theDPtr = GetNewDialog(INPUTDLOG, nil,(WindowPtr) -1L)))
{
FatalError("Can't allocate memory for window");
}
SetPort(theDPtr);
CenterWindow(theDPtr);
ShowWindow(theDPtr);
((DialogPeek) theDPtr)->aDefItem = 1;
GetDItem(theDPtr, 5, &type, &theItem, &theRect);
SetDItem(theDPtr, 5, type,(Handle) HiliteDefaultButton, &theRect);
GetDItem(theDPtr, 3, &type, &theItem, &theRect);
CtoPstr(thePrompt);
SetIText(theItem,(unsigned char *) thePrompt);
PtoCstr((unsigned char *) thePrompt);
GetDItem(theDPtr, 4, &type, &theItem, &theRect);
CtoPstr(theText);
SetIText(theItem,(unsigned char *) theText);
SelIText(theDPtr, 4, 0, 256);
PtoCstr((unsigned char *) theText);
if (GetNextEvent(updateMask,&theEvent))
{
if (theEvent.what == kHighLevelEvent)
{
DoHighLevel(&theEvent);
}
else if (theEvent.message == (long) theDPtr)
{
DialogSelect(&theEvent,&tempDPtr,&itemHit);
}
}
do
{
ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
} while (itemHit!=1 && itemHit!=2);
GetDItem(theDPtr, 4, &type, &theItem, &theRect);
if (itemHit==1)
{
GetIText(theItem,(unsigned char *) theText);
PtoCstr((unsigned char *) theText);
if (theText[0])
itemHit = noErr;
else
itemHit = -1;
}
else
{
theText[0] = 0;
itemHit = -1;
}
DisposDialog(theDPtr);
SetPort(oldPort);
return(itemHit);
}
OSErr YesNoDialog(char *thePrompt,int theDefault)
{
DialogPtr theDPtr,tempDPtr;
short itemHit, type;
Handle theItem;
Rect tempRect;
GrafPtr oldPort;
char tempString[256];
EventRecord theEvent;
GetPort(&oldPort);
InitCursor();
if (!(theDPtr = GetNewDialog(OKCANCELDLOG, nil,(WindowPtr) -1L)))
{
FatalError("Can't allocate memory for window");
}
SetPort(theDPtr);
CenterWindow(theDPtr);
ShowWindow(theDPtr);
((DialogPeek) theDPtr)->aDefItem = theDefault;
GetDItem(theDPtr, 4, &type, &theItem, &tempRect);
SetDItem(theDPtr, 4, type,(Handle) HiliteDefaultButton, &tempRect);
strcpy(tempString,thePrompt);
CtoPstr(tempString);
GetDItem(theDPtr, 3, &type, &theItem, &tempRect);
SetIText(theItem,(unsigned char *) tempString);
if (GetNextEvent(updateMask,&theEvent))
{
if (theEvent.what == kHighLevelEvent)
{
DoHighLevel(&theEvent);
}
else if (theEvent.message == (long) theDPtr)
{
DialogSelect(&theEvent,&tempDPtr,&itemHit);
}
}
do
{
ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
} while (itemHit!=1 && itemHit!=2);
DisposDialog(theDPtr);
SetPort(oldPort);
return(itemHit);
}